package lt.laimis.shop.service.product;

import java.util.List;
import java.util.ArrayList;

import lt.laimis.shop.dao.DaoNode;
import lt.laimis.shop.service.content.ContentService;
import lt.laimis.shop.service.content.Content;

/**
 * Created by IntelliJ IDEA.
 * User: laimis
 * Date: Jul 17, 2008
 * Time: 11:17:54 PM
 * To change this template use File | Settings | File Templates.
 */
public class ProductService {
    private String productQName;
    private List<Product> productsList;

    protected DaoNode daoNode;
    protected ContentService contentService;

    public void setDaoNode(DaoNode daoNode) {
        this.daoNode = daoNode;
    }

    public void setContentService(ContentService contentService) {
        this.contentService = contentService;
    }

    public List<Product> getProductsList() {
        if(productsList == null && daoNode != null ){
              productsList = daoNode.getListProduct();
              for(Product product: productsList){
                  if(contentService != null){
                     Content content = contentService.getByParentId(product.getNodeId());
                      if(content != null){
                      product.setContent(content);
                      }
                  }
              }
        }
        return productsList;
    }


    public Product getProductByNodeId(String nodeId) {     
        Product item = null;
        if (nodeId != null && getProductsList() != null) {
            for (Product product : productsList) {
                if (product.getNodeId().equals(nodeId)) {
                    item = product;
                }
            }
        }
        return item;
    }
    
    public List<Product> getProductByParentId(String nodeId) {     
        List<Product> item = null;
        if (nodeId != null && getProductsList() != null) {
            item = new ArrayList<Product>();
            for (Product product : productsList) {
                if (product.getParentId().equals(nodeId)) {
                    item.add(product);
                }
            }
        }
        return item;
    }    
    

    public String getProductQName() {
        return productQName;
    }

    public void setProductQName(String productQName) {
        this.productQName = productQName;
    }
    
}



